package com.gjsh.studentManager;

import java.util.Arrays;
import java.util.Scanner;

public class Studentview {
    /*
    后面反复使用键盘录入,所以我们没必要执行一次就new一次Sanner
    所以将Sanner对象放到成员位置
     */
    Scanner sc = new Scanner(System.in);

    /*
        长度为50的数组
        后面每个功能都需要使用数组,所以将数组放到成员位置
     */
    Student[] students = new Student[50];

    /*
        记入有多少学生
     */
    int count = 0;

    //展示页面调用对应的功能
    public void start() {
        while (true) {
            System.out.println("-----------------学生管理系统------------------");
            System.out.println("1.添加学生");
            System.out.println("2.修改学生");
            System.out.println("3.删除学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("请选择1~5");
            int num = sc.nextInt();
            System.out.println("--------------------------------");
            switch (num) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    updateStudent();
                    break;
                case 3:
                    deletStudent();
                    break;
                case 4:
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("退出系统");
                    break;
            }
        }
    }

    // 查询方法
    private void findAllStudent() {
//        System.out.println("查询功能");
        System.out.println("学号" + " " + "姓名" + " " + "年龄" + " " + "性别" + " ");
        Arrays.sort(students);
        if (count == 0) {
            System.out.println("班级没有学生");
            return;
        }
        for (int i = 0; i < count; i++) {
            System.out.println(students[i].getId() + "    " + students[i].getName() + "    " + students[i].getAge() + "    " + students[i].getSix());
        }
    }

    //    删除方法
    private void deletStudent() {
        System.out.println("删除功能");
    }

    //    修改方法
    private void updateStudent() {
        //   System.out.println("修改功能");
        System.out.println("请输入需要修改的学号");
        int id = sc.nextInt();
        int upDateIndex = ArrUtils.findIndexById(students, id, count);
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入性别");
        String six = sc.next();
        Student student = new Student(name, age, id, six);
        students[upDateIndex] = student;
    }

    // 添加方法
    private void addStudent() {
//        System.out.println("添加功能");
        System.out.println("请输入学号");
        int id = sc.nextInt();
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入性别");
        String six = sc.next();
        Student student = new Student(name, age, id, six);
        students[count] = student;
        System.out.println("添加成功");
        count++;
    }
}
